package org.example.ttms.controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import org.example.ttms.Main;
import org.example.ttms.customize.Pop_ups;
import org.example.ttms.entityClass.Account;
import org.example.ttms.entityClass.Listening;
import org.example.ttms.entityClass.Manager;
import org.example.ttms.entityClass.User;

import java.io.IOException;

public class LoginController {

    @FXML
    private Button login;

    @FXML
    private Button enroll;

    @FXML
    private TextField account;

    @FXML
    private PasswordField password;

    @FXML
    void chickEnroll(ActionEvent event) throws IOException {
        Main.jumpPage("enroll.fxml");
    }

    //登录评判
    public String loginJudgement(){
        Account account1 = new Account();
        Pop_ups popUps = new Pop_ups();
        //请输入账号
        if(account.getText().isEmpty()){
            popUps.pop_ups1(Main.scene, "请输入账号");
            return null;
        }
        //请输入密码
        if(password.getText().isEmpty()){
            popUps.pop_ups1(Main.scene, "请输入密码");
            return null;
        }
        if(!account1.accountIsExist(account.getText())){
            popUps.pop_ups1(Main.scene, "账号不存在");
            return null;
        }else{
            String type =  account1.judgePassword(account.getText(), password.getText());
            if(type == null)
                popUps.pop_ups1(Main.scene, "密码错误");
            return type;
        }
    }

    @FXML
    void chickLogin(ActionEvent event) throws IOException {
        String type = loginJudgement();
        if(type == null) {
            return;
        }
        Pop_ups popUps = new Pop_ups();
        if(type.equals("管理员")) {
            Main.jumpPage("manager.fxml");
            ManagerController.manager = new Manager().obtainManager(account.getText());
        }
        else{
            Main.jumpPage("user.fxml");
            UserController.user = new User().obtainUser(account.getText());
        }
        Main.stage.setX(100);
        Main.stage.setY(10);
        //开始监听
        Listening listening = new Listening();
        listening.start();
        popUps.pop_ups4("登录成功");
    }
}
